package client;



import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import server.SendToClient;
import server.Threads;



import common.ChatIF;

public class ClientConsole implements ChatIF 
{
  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this ConsoleChat.
   */
  ChatClient client;
  public Object result = null;
  public boolean flag = false;
  public static Threads_client checkUsers = new Threads_client();
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
 * @throws InterruptedException 
   */
  public ClientConsole(String host, int port)
  {
    try 
    {
    	client= new ChatClient(host, port, this);
    } 
    catch(IOException exception) 
    {
      System.out.println("Error: Can't setup connection!, Terminating client.");
      System.exit(1);
    }
  }
  //Instance methods ************************************************
  
  /**
   * This method waits for input from the console.  Once it is 
   * received, it sends it to the client's message handler.
   */
  public void accept(Object obj) 
  {
    try
    {
        client.handleMessageFromClientUI(obj);
    } 
    catch (Exception ex) 
    {
      System.out.println("Unexpected error while reading from console!");
    }
  }

  public void display(Object obj) 
  {  
	  if(obj instanceof SendToClient)
	  {		
		  checkUsers.start();
		  if(((SendToClient)obj).status.equals("FROZEN"))
		  {
			  JOptionPane.showMessageDialog(null,"Your account has been frozen, please contact your system administrator for details","ERROR",JOptionPane.ERROR_MESSAGE);	 
			  checkUsers.frozen.add(((SendToClient)obj).uid);
		  }
	  }
	  else
	  {
		  result = obj;	 
		  flag = true;
	  }
  }
  
  public Object getResult()
  {
  	return result;
  }
}
